package main

import "encoding/json"

type Student struct {
	Id     string `json:"id"`
	Name   string `json:"name"`
	Age    int    `json:"age"`
	Gender string `json:"gender"`
	Class  string `json:"class"`
	Grade  int    `json:"grade"`
}

func main() {
	lily := Student{
		Id:     "1",
		Name:   "lily",
		Age:    18,
		Gender: "female",
		Class:  "1",
		Grade:  1,
	}

	// 编码
	jsonByte, err := json.Marshal(&lily)
	if err != nil {
		panic(err)
	}
	println(string(jsonByte))

	// 解码
	var lily2 Student
	err = json.Unmarshal(jsonByte, &lily2)
	if err != nil {
		panic(err)
	}
	println("Name:", lily2.Name)
	println("Age:", lily2.Age)
	println("Gender:", lily2.Gender)
	println("Class:", lily2.Class)
	println("Grade:", lily2.Grade)
}
